package com.qst.rongxiaoserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.rongxiaoserver.common.Result;
import com.qst.rongxiaoserver.model.BankModel;
import com.qst.rongxiaoserver.model.dto.BankDto;
import com.qst.rongxiaoserver.service.IBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/bank")
@Tag(name = "银行管理", description = "银行管理相关接口")
public class BankController {

    @Autowired
    IBankService bankService;

    // 添加银行
    @PostMapping("/add")
    @Operation(summary = "添加银行", description = "添加银行")
    public Result<BankModel> addBank(@Valid @RequestBody BankModel bankModel) {
       boolean success =   bankService.save(bankModel);
       return success? Result.success(bankModel,"添加成功"):Result.fail("添加失败");
    }

    // 修改银行
    @PostMapping("/update")
    @Operation(summary = "编辑银行信息", description = "编辑银行")
    public Result<BankModel> updateBank(@Valid @RequestBody BankModel bankModel) {
        boolean success =   bankService.updateById(bankModel);
        return success? Result.success(bankModel,"编辑成功"):Result.fail("编辑失败");
    }

    // 查询银行
    @GetMapping("/list")
    @Operation(summary = "银行列表", description = "银行列表")
    public Result<Page<BankModel>> getBanks(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "bankName", required = false) String bankName) {

       Page<BankModel> bankList =  bankService.getBankList(pageNum, pageSize, bankName);
        return bankList!=null ? Result.success(bankList,"查询成功"):Result.fail("查询失败");
    }

    @GetMapping("/list2")
    @Operation(summary = "银行列表2", description = "银行列表")
    public String getBanks2(@ModelAttribute @Valid BankDto bankDto) {
       String name =  bankDto.getBankName();
       Integer pageNum = bankDto.getPageNum();
       Integer pageSize = bankDto.getPageSize();
        return "dddddd";

    }


    // 删除银行
    @DeleteMapping("/delete")
    @Operation(summary = "根据ID删除银行", description = "根据ID删除银行")
    public Result deleteBank(@Valid @RequestParam Integer bankId) {
        boolean success =   bankService.removeById(bankId);
        return success? Result.success("删除成功"):Result.fail("删除失败");
    }

  // 获取银行详情
    @GetMapping("/detail")
    @Operation(summary = "根据ID获取银行详情", description = "根据ID获取银行详情")
    public Result<BankModel> detailBank(@Valid @RequestParam Integer bankId) {
        BankModel bankModel =  bankService.getById(bankId);
        return bankModel!=null? Result.success(bankModel,"查询成功"):Result.fail("查询失败");
    }
}